package com.yanqu.road.server.manager.activity.zhenhuan;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.zhenhuan.eZhenHuanChildType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanFirstProcessConfig;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanSecondProcessConfig;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanThirdProcessConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.sync.message.Service;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossZhenHuanMgr extends TempCrossMgr {
    private static Map<Integer, CrossZhenHuanPromotionActivity> promotionActivityMap = new ConcurrentHashMap<>();

    private static Map<Integer, CrossZhenHuanSecondActivity> secondActivityMap = new ConcurrentHashMap<>();

    /**
     * 甄嬛三阶段
     */
    private static Map<Integer, CrossZhenHuanThirdProcessActivity> thirdProcessActivityMap = new ConcurrentHashMap<>();

    public static void initService(Service syncMessageService) {

    }

    @Override
    public boolean removeExpireData() throws Exception {
        long currentSecond = DateHelper.getCurrentTime() / 1000;
        List<Integer> needRemoveIdList = new ArrayList<>();
        for (CrossZhenHuanPromotionActivity activity : promotionActivityMap.values()) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activity.getActivityId());
            if (activityInfo == null || currentSecond > activityInfo.getEndShowTime() + 1 * DateHelper.HOUR_SECONDS) {
                needRemoveIdList.add(activity.getActivityId());
            }
        }

        for (Integer activityId : needRemoveIdList) {
            promotionActivityMap.remove(activityId);
        }
        needRemoveIdList.clear();

        for (CrossZhenHuanSecondActivity activity : secondActivityMap.values()) {
            ZhenHuanSecondProcessConfig secondProcessConfig = CrossZhenHuanConfigMgr.getSecondProcessConfig(activity.getActivityInfo().getActivityId());
            if (secondProcessConfig == null) {
                needRemoveIdList.add(activity.getActivityInfo().getActivityId());
                continue;
            }
            ActivityInfo activityInfo = activity.getActivityInfo();
            if (currentSecond > activityInfo.getEndShowTime() + 1 * DateHelper.HOUR_SECONDS) {
                needRemoveIdList.add(activityInfo.getActivityId());
            }
        }

        for (Integer activityId : needRemoveIdList) {
            secondActivityMap.remove(activityId);
        }
        needRemoveIdList.clear();

        for (CrossZhenHuanThirdProcessActivity activity : thirdProcessActivityMap.values()) {
            ZhenHuanThirdProcessConfig thirdProcessConfig = CrossZhenHuanConfigMgr.getThirdProcessConfig(activity.getActivityId());
            if (thirdProcessConfig == null || currentSecond > thirdProcessConfig.getActivityInfo().getEndShowTime() + 1 * DateHelper.HOUR_SECONDS) {
                needRemoveIdList.add(activity.getActivityId());
            }
        }

        for (Integer activityId : needRemoveIdList) {
            thirdProcessActivityMap.remove(activityId);
        }

        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadActivityConfig();
        return true;
    }

    public boolean save() {
        for (CrossZhenHuanPromotionActivity activity : promotionActivityMap.values()) {
            activity.save();
        }

        for (CrossZhenHuanSecondActivity activity : secondActivityMap.values()) {
            activity.save();
        }

        //甄嬛3数据存储
        for (CrossZhenHuanThirdProcessActivity thirdProcessActivity : thirdProcessActivityMap.values()) {
            thirdProcessActivity.save();
        }
        return true;
    }

    public static void reloadActivityConfig() {
        getLogger().info("reload CrossZhenHuanMgr start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.ZhenHuan.getValue());

        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadZhenHuanData(item);
        }
        getLogger().info("reload CrossZhenHuanMgr end");
    }

    /**
     * 加载甄嬛数据
     *
     * @param activityInfo 活动信息
     */
    private static void reloadZhenHuanData(ActivityInfo activityInfo) {
        if (activityInfo.getChildType() == eZhenHuanChildType.First.getValue()) {
            getPromotionActivity(activityInfo);
        } else if (activityInfo.getChildType() == eZhenHuanChildType.Second.getValue()) {
            getSecondActivity(activityInfo.getActivityId());
        } else if (activityInfo.getChildType() == eZhenHuanChildType.Third.getValue()) {
            if (!thirdProcessActivityMap.containsKey(activityInfo.getActivityId())) {
                getThirdProcessActivity(activityInfo.getActivityId());
            }
        }
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 晋升任务 第一阶段 第二阶段 进入领奖期要同步晋升数据
     */
    public static void promotionTask() {
        for (CrossZhenHuanPromotionActivity promotionActivity : promotionActivityMap.values()) {
            promotionActivity.promotionTask();
        }
    }

    public static void syncPromotionTask() {
        for (CrossZhenHuanPromotionActivity promotionActivity : promotionActivityMap.values()) {
            promotionActivity.syncPromotionTask();
        }
    }

    public static CrossZhenHuanPromotionActivity getPromotionActivity(ActivityInfo activityInfo) {
        int activityId = activityInfo.getActivityId();
        CrossZhenHuanPromotionActivity activity = promotionActivityMap.get(activityId);
        if (activity == null) {
            synchronized (promotionActivityMap){
                activity = promotionActivityMap.get(activityId);
                if (activity == null) {
                    if (activityInfo.getChildType() == eZhenHuanChildType.First.getValue()) {
                        ZhenHuanFirstProcessConfig firstProcessConfig = CrossZhenHuanConfigMgr.getFirstProcessConfig(activityId);
                        if (firstProcessConfig == null) {
                            CrossZhenHuanConfigMgr.reloadZhenHuanConfig(activityInfo);
                        }
                        firstProcessConfig = CrossZhenHuanConfigMgr.getFirstProcessConfig(activityId);
                        if (firstProcessConfig == null) {
                            return null;
                        }
                    } else if (activityInfo.getChildType() == eZhenHuanChildType.Second.getValue()) {
                        ZhenHuanSecondProcessConfig secondProcessConfig = CrossZhenHuanConfigMgr.getSecondProcessConfig(activityId);
                        if (secondProcessConfig == null) {
                            CrossZhenHuanConfigMgr.reloadZhenHuanConfig(activityInfo);
                        }
                        secondProcessConfig = CrossZhenHuanConfigMgr.getSecondProcessConfig(activityId);
                        if (secondProcessConfig == null) {
                            return null;
                        }
                    }
                    activity = new CrossZhenHuanPromotionActivity(activityInfo);
                    promotionActivityMap.put(activityId, activity);
                }
            }
        }
        return activity;
    }

    public static Map<Integer, CrossZhenHuanThirdProcessActivity> getThirdProcessActivityMap() {
        return thirdProcessActivityMap;
    }

    /**
     * 获取第三阶段活动
     *
     * @param activityId 活动Id
     */
    public static CrossZhenHuanThirdProcessActivity getThirdProcessActivity(int activityId) {
        CrossZhenHuanThirdProcessActivity activity = thirdProcessActivityMap.get(activityId);
        if (activity == null) {
            synchronized (thirdProcessActivityMap) {
                activity = thirdProcessActivityMap.get(activityId);
                if (activity == null) {
                    ZhenHuanThirdProcessConfig thirdProcessConfig = CrossZhenHuanConfigMgr.getThirdProcessConfig(activityId);
                    if (thirdProcessConfig == null) {
                        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                        if (activityInfo == null) {
                            getLogger().error("getThirdProcessActivity activityInfo is null activityId:{}", activityId);
                            return null;
                        }
                        CrossZhenHuanConfigMgr.reloadZhenHuanConfig(activityInfo);
                        thirdProcessConfig = CrossZhenHuanConfigMgr.getThirdProcessConfig(activityInfo.getActivityId());
                        if (thirdProcessConfig == null) {
                            getLogger().error("getThirdProcessActivity thirdProcessConfig is null activityId:{}", activityId);
                            return null;
                        }
                    }
                    // 初始化一个
                    activity = new CrossZhenHuanThirdProcessActivity(thirdProcessConfig.getActivityInfo());
                    thirdProcessActivityMap.put(activityId, activity);
                }
            }
        }
        return activity;
    }

    /**
     * 获取开宴次数
     * @param activityId
     * @param userId
     * @return
     */
    public static int getUserBanquetCount(int activityId, long userId) {
        CrossZhenHuanThirdProcessActivity activity = thirdProcessActivityMap.get(activityId);
        if (activity != null) {
            return activity.getUserBanquetCount(userId);
        }
        return 0;
    }

    public static CrossZhenHuanSecondActivity getSecondActivity(int activityId) {
        CrossZhenHuanSecondActivity activity = secondActivityMap.get(activityId);
        if (activity == null) {
            synchronized (secondActivityMap) {
                activity = secondActivityMap.get(activityId);
                if (activity == null) {
                    // 先判断下活动的有效性
                    ZhenHuanSecondProcessConfig secondProcessConfig = CrossZhenHuanConfigMgr.getSecondProcessConfig(activityId);
                    if (secondProcessConfig == null) {
                        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                        if (activityInfo == null) {
                            getLogger().error("CrossZhenHuanSecondActivity not found ,activityId:{}", activityId);
                            return null;
                        }
                        CrossZhenHuanConfigMgr.reloadZhenHuanConfig(activityInfo);
                        secondProcessConfig = CrossZhenHuanConfigMgr.getSecondProcessConfig(activityId);
                        if (secondProcessConfig == null) {
                            getLogger().error("CrossZhenHuanSecondActivity not found ,activityId:{}", activityId);
                            return null;
                        }
                    }
                    if (!ActivityHelper.activityInShowTime(secondProcessConfig.getActivityInfo())) {
                        getLogger().error("CrossZhenHuanSecondActivity not in show time ,activityId:{}", activityId);
                        return null;
                    }
                    // 初始化一个
                    activity = new CrossZhenHuanSecondActivity(secondProcessConfig);

                    getPromotionActivity(secondProcessConfig.getActivityInfo());

                    secondActivityMap.put(activityId, activity);
                }
            }
        }
        return activity;
    }

    public static void secondActivityPartyTask() {
        for (CrossZhenHuanSecondActivity activity : secondActivityMap.values()) {
            activity.checkPartyEndTask();

            activity.syncEndPartyTask();
        }
    }
}
